<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/index.css" />
  </head>
  <body>
    <div class="container">
      <ul class="top">
        <!-- 基础模板 -->
        <li>
          <a href="javascript:;">首页</a>
          <ul class="sub">
            <li>
              <a href="javascript:;">
                <span>砂锅厨具</span>
                <img
                  src="https://yanxuan.nosdn.127.net/3102b963e7a3c74b9d2ae90e4380da65.png?quality=95&imageView"
                  alt=""
                />
              </a>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <script src="./axios.js"></script>
    <script>
      async function getList() {
        // 请求接口
        // 一级分类
        const topRes = await axios({
          url: 'http://ajax-api.itheima.net/api/category/top',
        })
        // 一级分类的数组
        console.log('topRes', topRes)
        // 二级分类 twoPros是一个数组，数组的每一项都是一个promise
        const twoPros = topRes.data.data.map((item) => {
          return axios({
            url: 'http://ajax-api.itheima.net/api/category/sub',
            params: {
              id: item.id,
            },
          })
        })
        // 二级分类的数组
        const twoRes = await Promise.all(twoPros)
        console.log('twoRes', twoRes)

        // 渲染页面
        const htmlStr = twoRes
          .map((item) => {
            return `<li>
          <a href="javascript:;">${item.data.data.name}</a>
          <ul class="sub">
              ${item.data.data.children
                .map((child) => {
                  return `<li>
                            <a href="javascript:;">
                              <span>${child.name}</span>
                              <img
                                src="${child.picture}"
                                alt=""
                              />
                            </a>
                          </li>`
                })
                .join('')}
          </ul>
        </li>`
          })
          .join('')

        document.querySelector('.top').innerHTML = htmlStr
      }
      window.onload = function () {
        getList()
      }
    </script>
  </body>
</html>
